import {Message} from "element-ui";

export default function ({ $axios, redirect }) {
  $axios.interceptors.request.use(config=>{
    config.headers.common['token'] = window.localStorage.getItem("token");
    return config;
  });
  $axios.interceptors.response.use(res => {
    // 返回数据逻辑处理

    let data = res.data;
    switch (data.status){
      case 200:
        return data;
      case 201:
        return data;
      case 400:
        Message({
          type:"error",
          offset:80,
          message:data.message,
          center:true
        })
        break;
      case 401:
        Message({
          type:"error",
          offset:80,
          message:data.message,
          center:true
        });
        localStorage.removeItem("token");
        break;
      case 404:
        Message({
          type:"error",
          offset:80,
          message:data.message,
          center:true
        })
        break;
      case 405:
        Message({
          type:"error",
          offset:80,
          message:data.message,
          center:true
        })
        break;
      case 10000:
        Message({
          type:"error",
          offset:80,
          message:data.message,
          center:true
        })
        break;
      default:
        Message({
          type:"error",
          offset:80,
          message:data.message || '未知错误，请联系管理员！！！',
          center:true
        })
    }
    return Promise.reject(data);
  });
}